home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / bash / bash_108 / src_d2.zoo / bash-d2.zoo / libgcc / open.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-09-03  |  3.4 KB  |  154 lines

  1. /*
  2.  *        Cross Development System for Atari ST 
  3.  *     Copyright (c) 1988, Memorial University of Newfoundland
  4.  *
  5.  *  Tries to emulate unix open3.  The mode argument is ignored, and O_NDELAY
  6.  * doesn't do anything.  Also O_APPEND does not garentee that things will
  7.  * be written to the end - all we can do is seek to the end to start with.
  8.  *
  9.  * The above comments are no longer (necessarily) accurate; this file
  10.  * has been extensively modified by jrd, ers, jrb, and others. See ChangeLog
  11.  * for some of the details.
  12.  */
  13.  
  14. #include    <stdio.h>
  15. #include    <stdlib.h>
  16. #include    <string.h>
  17. #include    <types.h>
  18. #include    <fcntl.h>
  19. #include    <errno.h>
  20. #include    <osbind.h>
  21. #include    <support.h>
  22. #include    <unistd.h>
  23. #include    <memory.h>
  24. #include    <assert.h>
  25. #include    <stat.h>
  26. #include    <device.h>
  27. #include     <stdarg.h>
  28. #include    "symdir.h"
  29. #include    "lib.h"
  30.  
  31. int __umask = 0;
  32.  
  33. #ifdef __STDC__
  34. int open(const char *_path, int flags, ...)
  35. #else
  36. int open(_path, flags)
  37.     const char    *_path;
  38.     int        flags;
  39. #endif
  40. {
  41.   int            fd, ifd;
  42.   int            exists;
  43.   char             path[FILENAME_MAX];
  44.   unsigned short    create_mode = 0;
  45.   int            name_munged;
  46.   struct _device    *dev;
  47.   unsigned int        mode;
  48.   va_list        argp;
  49.  
  50.   va_start(argp,flags);
  51.   mode = ((unsigned int)(va_arg(argp, int))) & ~__umask;
  52.  
  53. /* convert Unix style filename to GEMDOS style */
  54. /* if the name gets munged, unx2dos will return _NM_CHANGE,
  55.  * and will copy the original name to the global array __link_name[]
  56.  */
  57.  
  58.   name_munged = unx2dos(_path, path);
  59.  
  60.   if (name_munged == _NM_DEV && (dev = _dev_dosname(path)) && dev->open) {
  61.     return (*dev->open)(path, flags, mode);
  62.   }
  63.  
  64.   switch (flags & 0x3) {
  65.     case O_RDONLY:
  66.         fd = Fopen(path, O_RDONLY);
  67.         break;
  68.  
  69.     case O_WRONLY:
  70.     case O_RDWR:
  71.         fd = 0;
  72.         exists = Fattrib(path, 0, 0) >= 0;
  73.         if (flags & O_CREAT) {
  74.             if ((flags & O_EXCL) && exists)
  75.                 fd = - EEXIST;
  76.         }
  77.         else
  78.             if (!exists)
  79.                 fd = - ENOENT;
  80.         if (!fd) {
  81.             if ((flags & O_TRUNC) || !exists) {
  82.  
  83.                 if (!(mode & S_IREAD))
  84.                     create_mode |= FA_HIDDEN;
  85.                 if (!(mode & S_IWRITE))
  86.                     create_mode |= FA_RDONLY;
  87.  
  88.                 fd = Fcreate(path, create_mode);
  89.                 if (fd >= 0) {
  90.                     if (name_munged == _NM_CHANGE)
  91.                         _make_autolink(path, __link_name);
  92.                     if ((flags & 0x3) == O_RDWR) {
  93.                         (void) Fclose(fd);
  94.                         fd = Fopen(path, O_RDWR);
  95.                     }
  96.                 }
  97.             } else {
  98.                 if ((fd = Fopen(path, flags & 0x3)) >= 0 &&
  99.                     (flags & O_APPEND))
  100.                     (void) lseek(fd, 0L, SEEK_END);
  101.             }
  102.         }
  103.         break;
  104.  
  105.     default:
  106.         fd = - EINVAL;
  107.   }
  108.  
  109.   if (fd < __SMALLEST_VALID_HANDLE) {
  110.     errno = - fd;
  111.     fd = __SMALLEST_VALID_HANDLE  - 1;
  112.     return fd;
  113.   }
  114.  
  115.   ifd = __OPEN_INDEX(fd);
  116.   assert(( ((ifd >= 0) && (ifd < __NHANDLES)) &&
  117.        (__open_stat[ifd].status == FH_UNKNOWN) ));
  118.   if(flags & O_NDELAY)
  119.     __open_stat[ifd].nodelay = 1;
  120.   if(flags & O_APPEND)
  121.     __open_stat[ifd].append = 1;
  122.   if(flags & O_PIPE)
  123.         __open_stat[ifd].pipe = 1;
  124.  
  125.   if((__open_stat[ifd].filename = strdup(path)) == NULL) {
  126.         int tmp = errno;
  127.          __open_stat[ifd].status = FH_UNKNOWN;
  128.         (void)close(fd);
  129.         if((!exists) && (flags & O_CREAT)) /* if we created a file */
  130.             (void)unlink(path);
  131.         errno = tmp;
  132.         fd = __SMALLEST_VALID_HANDLE - 1;
  133.   }
  134.   return fd;
  135. }
  136.  
  137. int
  138. creat(name, mode)
  139.     const char *name;
  140.     unsigned   mode;
  141. {
  142.     return open(name, O_WRONLY|O_CREAT|O_TRUNC, mode);
  143. }
  144.  
  145. /* umask -- change default file creation mask */
  146.  
  147. int umask(complmode)
  148.        int complmode;
  149. {
  150.     int old_umask = __umask;
  151.     __umask = complmode;
  152.     return old_umask;
  153. }
  154.